package com.lp.service.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.iframework.commons.domain.enums.RecordStatus;
import org.iframework.commons.domain.order.Order;
import org.iframework.commons.domain.order.OrderImpl;
import org.iframework.commons.domain.pager.Pager;
import org.iframework.commons.domain.pager.PagerImpl;
import org.iframework.commons.util.fast.L;
import org.iframework.commons.util.fast.V;
import org.iframework.support.spring.controller.BaseControllerSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lp.business.domain.Result;
import com.lp.business.model.Collection;
import com.lp.business.model.Member;
import com.lp.business.model.Report;
import com.lp.business.service.CollectionService;
import com.lp.business.service.MemberService;
import com.lp.business.service.ReportService;

/**
 * 收藏
 * 
 * @author lipeng
 * @date 2018年08月22日 下午1:15:45
 */
@RestController
@RequestMapping("/api/collection")
public class CollectionController extends BaseControllerSupport {

	@Autowired
	private CollectionService collectionService;
	@Autowired
	private ReportService reportService;
	@Autowired
	private MemberService memberService;

	/**
	 * 收藏报告 <br>
	 */
	@RequestMapping(value = "collect/{reportId}", method = { RequestMethod.POST, RequestMethod.GET })
	public Result collect(HttpServletRequest request, final HttpServletResponse response, @PathVariable String reportId) {
		try {
			String memberId = (String)request.getAttribute("memberId");
			if(V.isEmpty(memberId)) {
				return Result.error("登录状态失效");
			}
			Member member = memberService.get(memberId);
			if(V.isEmpty(member)) {
				return Result.error("登录状态失效");
			}
			if(V.isEmpty(reportId)) {
				return Result.error("报告id未传入");
			}
			Report report = reportService.get(reportId);
			if(V.isEmpty(report)) {
				return Result.error("报告id非法，收藏失败");
			}
			Collection model = new Collection();
			model.setMemberId(member.getId());
			model.setReportId(report.getId());
			model.setStatus(RecordStatus.AVAILABLE);
			collectionService.save(model);
			return Result.ok();
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("接口异常");
		}
	}
	
	/**
	 * 取消收藏报告 <br>
	 */
	@RequestMapping(value = "cancel/{reportId}", method = { RequestMethod.POST, RequestMethod.GET })
	public Result cancel(HttpServletRequest request, final HttpServletResponse response, @PathVariable String reportId) {
		try {
			String memberId = (String)request.getAttribute("memberId");
			if(V.isEmpty(memberId)) {
				return Result.error("登录状态失效");
			}
			Member member = memberService.get(memberId);
			if(V.isEmpty(member)) {
				return Result.error("登录状态失效");
			}
			if(V.isEmpty(reportId)) {
				return Result.error("报告id未传入");
			}
			Report report = reportService.get(reportId);
			if(V.isEmpty(report)) {
				return Result.error("报告id非法，取消失败");
			}
			Collection model = new Collection();
			model.setMemberId(member.getId());
			model.setReportId(report.getId());
			model.setStatus(RecordStatus.AVAILABLE);
			List<Collection> models = collectionService.findByModel(model, null, null);
			if(collectionService.deleteAllVirtual(models)) {
				return Result.ok();
			}else {
				return Result.error();
			}
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("接口异常");
		}
	}
	
	/**
	 * 获取收藏列表 <br>
	 */
	@RequestMapping(value = "list", method = { RequestMethod.POST, RequestMethod.GET })
	public Result list(HttpServletRequest request, final HttpServletResponse response) {
		try {
			String memberId = (String)request.getAttribute("memberId");
			if(V.isEmpty(memberId)) {
				return Result.error("登录状态失效");
			}
			Member member = memberService.get(memberId);
			if(V.isEmpty(member)) {
				return Result.error("登录状态失效");
			}
			Collection model = new Collection();
			model.setMemberId(member.getId());
			model.setStatus(RecordStatus.AVAILABLE);
			Order order = new OrderImpl(request);
			Pager pager = new PagerImpl(request);
			List<Map<String, Object>> collections = collectionService.findCollections(model, order, pager);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("collections", collections);
			return Result.ok().put("data", data);
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("接口异常");
		}
	}
	
}
